package com.ruoyi.purchase.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.RawProductDetail;
import com.ruoyi.purchase.domain.SupplyDetail;
import com.ruoyi.purchase.domain.SupplyDetailParams;
import com.ruoyi.purchase.service.IRawProductDetailService;
import com.ruoyi.purchase.service.ISupplyDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * supplyDetailController
 * 
 * @author kevinness
 * @date 2025-01-15
 */
@RestController
@RequestMapping("/supplyDetail")
public class SupplyDetailController extends BaseController
{
    @Autowired
    private ISupplyDetailService supplyDetailService;

    @Autowired
    private IRawProductDetailService rawProductDetailService;

    /**
     * 查询supplyDetail列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SupplyDetail supplyDetail)
    {
        startPage();
        List<SupplyDetail> list = supplyDetailService.selectSupplyDetailList(supplyDetail);
        return getDataTable(list);
    }

    /**
     * 根据多个货品批号获取货源详情列表
     */
    @PostMapping("/batches")
    public AjaxResult getListByBatches(@RequestBody List<String> batches){
        List<SupplyDetailParams> list = new ArrayList<SupplyDetailParams>();
        for(String batch : batches){
            SupplyDetail supplyDetail = supplyDetailService.selectSupplyDetailByRawProductBatch(batch);
            if(supplyDetail != null){
                List<RawProductDetail> rawProductDetails = rawProductDetailService.selectRawProductDetailListBySupplyDetail(supplyDetail);
                list.add(new SupplyDetailParams(supplyDetail,rawProductDetails));
            }
        }
        return success(list);
    }

    /**
     * 导出supplyDetail列表
     */
    @Log(title = "supplyDetail", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupplyDetail supplyDetail)
    {
        List<SupplyDetail> list = supplyDetailService.selectSupplyDetailList(supplyDetail);
        ExcelUtil<SupplyDetail> util = new ExcelUtil<SupplyDetail>(SupplyDetail.class);
        util.exportExcel(response, list, "supplyDetail数据");
    }

    /**
     * 获取supplyDetail详细信息
     */
    @GetMapping(value = "/{rawProductBatch}")
    public AjaxResult getInfo(@PathVariable("rawProductBatch") String rawProductBatch)
    {
        return success(supplyDetailService.selectSupplyDetailByRawProductBatch(rawProductBatch));
    }

    /**
     * 新增supplyDetail
     */
    @Log(title = "supplyDetail", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SupplyDetail supplyDetail)
    {
        return toAjax(supplyDetailService.insertSupplyDetail(supplyDetail));
    }

    /**
     * 批量添加货源详情
     */
    @PostMapping("/addList")
    public AjaxResult addSupplyDetailList(@RequestBody List<SupplyDetail> supplyDetailList){
        return toAjax(supplyDetailService.insertSupplyDetailList(supplyDetailList));
    }

    /**
     * 修改supplyDetail
     */
    @Log(title = "supplyDetail", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SupplyDetail supplyDetail)
    {
        return toAjax(supplyDetailService.updateSupplyDetail(supplyDetail));
    }

    /**
     * 批量更新货源详情
     */
    @PostMapping("/updateList")
    public AjaxResult updateSupplyDetailList(@RequestBody List<SupplyDetail> supplyDetailList){
        return toAjax(supplyDetailService.updateSupplyDetailList(supplyDetailList));
    }

    /**
     * 删除supplyDetail
     */
    @Log(title = "supplyDetail", businessType = BusinessType.DELETE)
    @DeleteMapping("/{rawProductBatchs}")
    public AjaxResult remove(@PathVariable String[] rawProductBatchs)
    {
        return toAjax(supplyDetailService.deleteSupplyDetailByRawProductBatchs(rawProductBatchs));
    }

    /**
     * 根据调拨编号获取货源详情
     */
    @PostMapping("/list/allocationNo")
    public AjaxResult getSupplyDetailByAllocationNo(@RequestBody String allocationNo){
        return success(supplyDetailService.getSupplyDetailListByAllocationNo(allocationNo));
    }
}
